package com.zjj.spring.read.xml.processor;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.core.Ordered;

/**
 * 默认在applicationContext.xml中注释了该bean
 * 
 * <P>在调用初始化方法前后对Bean进行额外的处理</P>
 * <P>Bean后置处理器对IOC容器里的所有Bean实例逐一处理，而非单一实例。典型的应用是：检查Bean属性的正确性或根据特定的标准更改Bean的属性</P>
 * <P>在初始化方法被调用前后，Spring经把每个Bean实例分别传递给上述接口的以下两个方法</P>
 * {@link BeanPostProcessor#postProcessBeforeInitialization}
 * <br/>
 * {@link BeanPostProcessor#postProcessAfterInitialization}
 * <br/>
 * 一个例子就是RequiredAnnotationBeanPostProcessor-这是一个BeanPostProcessor实现，
 * 确保用（任意）注解标记的那些JavaBean属性确实被注入一个值
 * @author zhuangjiajie778
 */
public class MyBeanPostProcessor implements BeanPostProcessor , Ordered{

	/**
	 * 在容器的初始化方法(比如InitializingBean的afterPropertiesSet()和任何生命的初始化方法)被调用之前
	 */
	@Override
	public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
		System.out.println("【B】---------------Enter MyBeanPostProcessor.postProcessBeforeInitialization() from BeanPostProcessor---------------");
		System.out.println("【B】beanName = " + beanName + "  bean = " + bean);
		System.out.println("【B】---------------Exit  MyBeanPostProcessor.postProcessBeforeInitialization() from BeanPostProcessor---------------");
		System.out.println();		
		return bean;
	}

	/**
	 * 任何bean实例化回调之后
	 */
	@Override
	public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
		System.out.println("【A】---------------Enter MyBeanPostProcessor.postProcessAfterInitialization() from BeanPostProcessor---------------");
		System.out.println("【A】beanName = " + beanName + "  bean = " + bean);
		System.out.println("【A】---------------Exit  MyBeanPostProcessor.postProcessAfterInitialization() from BeanPostProcessor---------------");
		System.out.println();		
		return null;
	}

	@Override
	public int getOrder() {
		return 0;
	}

}
